<?php

namespace App\Service;

use App\Common\Utill\OperationLog;
use App\Models\Banners;
use Illuminate\Support\Facades\Log;

class BannersService extends CommonService
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getListData($request, $num)
    {
        $model = new Banners();
        $query = $model::query();
        $accountAuthType = self::accountAuthType();
        //网站
        $websiteId = $request->input('websiteId', '');
        if ($websiteId) {
            $query->where('website_id', $websiteId);
        } else {
            if($accountAuthType != 1){
                $websiteArr = WebsitesAdminService::getAdminWebsites();
                if($websiteArr){
                    $query->whereIn('website_id', $websiteArr);
                }else{
                    $query->where('website_id', -1);
                }
            }
        }
        //状态
        $status = $request->input('status', '');
        if ($status) {
            $query->where('status', $status);
        }

        $query->where("is_del", 1);
//        $query->orderBy('sort', 'asc');
        $query->orderBy('id', 'desc');
        //网站信息
        $websiteData = WebsitesService::getWebsiteData(2, 1);
        $result = $query->paginate($num)->toArray();
        foreach ($result['data'] as $key => $val) {
            $result['data'][$key]['website_name'] = $websiteData[$val['website_id']] ?? '';
        }
        return $this->success('SUCCESS', $result['total'], $result['data']);
    }


    /**
     * 添加数据保存
     * @return void
     */
    public function store($websiteId, $jump_url, $sort, $thumbs,$titles,$describes)
    {
        $model = new Banners();
        $model->website_id = $websiteId;
        $model->thumbs = $thumbs;
        $model->sort = $sort ?? 1;
        $model->jump_url = $jump_url;
        $model->admin_id = $this->adminId;
        $model->titles = $titles ?? "";
        $model->describes = $describes ?? "";
        $model->created_at = date("Y-m-d H:i:s");
        if (!$model->save()) {
            return $this->error('添加失败！！！');
        } else {
            OperationLog::record(1,'轮播图管理-添加',$model->id,$model->toArray());
            return $this->success('添加成功');
        }
    }

    /**
     * 编辑
     * @param $data
     * @return void
     */
    public function update($id,$websiteId, $jump_url, $sort, $thumbs,$titles,$describes)
    {
        $oldData = Banners::query()->where('id', $id)->first()->toArray();
        $model = Banners::findOrFail($id);
        $model->website_id = $websiteId;
        $model->jump_url = $jump_url;
        $model->thumbs = $thumbs;
        $model->sort = $sort;
        $model->titles = $titles;
        $model->describes = $describes;
        $model->updated_at = date("Y-m-d H:i:s");
        if (!$model->save()) {
            return $this->error('修改失败');
        }
        OperationLog::record(2,'轮播图管理-编辑',$id,$model->toArray(),$oldData);
        return $this->success("修改成功");
    }

    /**
     * 释放
     * @param $id
     * @return void
     */
    public function release($id)
    {
        $result = Banners::findOrFail($id);
        $result->admin_id = 0;
        $result->website_id = 0;
        $result->operation_id = $this->adminId;
        $result->updated_at = date("Y-m-d H:i:s");
        if (!$result->save()) {
            return $this->error('操作失败');
        }
        return $this->success("操作成功");
    }

    /**
     * 状态修改
     * @return void
     */
    public function status($id)
    {
        $isShow = Banners::query()->where(['id'=>$id])->value('status');
        $data['status'] = $isShow == 1 ? -1 : 1;
        $data['updated_at'] = date('Y-m-d H:i:s');
        $result = Banners::query()->where('id', $id)->update($data);
        if (!$result) {
            return $this->error('设置失败');
        }
        OperationLog::record(2,'轮播图管理-状态修改',$id,['status'=>$data['status']],['status'=>$isShow]);
        return $this->success('设置成功');
    }


    /**
     * 单个删除
     */
    public function delOne($id)
    {
        $result = Banners::findOrFail($id);
        $result->is_del = -1;
        $result->updated_at = date("Y-m-d H:i:s");
        if (!$result->save()) {
            return $this->error('删除失败');
        }
        OperationLog::record(3,'轮播图管理-删除',$id);
        return $this->success("删除成功");
    }

    /**
     * 批量删除
     * @return void
     */
    public function delAll($idArr)
    {
        if (!$idArr) {
            return $this->error('删除内容不能为空');
        }
        Log::info("删除id:", $idArr);
        $delData['is_del'] = -1;
        $delData['updated_at'] = date("Y-m-d H:i:s");
        $result = Banners::query()->whereIn("id", $idArr)->update($delData);
        if (!$result) {
            return $this->error('删除失败');
        }
        OperationLog::record(3,'轮播图管理-删除',0,'','',implode(",", $idArr));
        return $this->success("删除成功");
    }


}
